using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SunshineFarmWord
{
    public class SystemObserverManage
    {
        //单利
        private static SystemObserverManage _instance;
        public static SystemObserverManage Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new SystemObserverManage();
                }
                return _instance;
            }
        }

        private Dictionary<GameObjEnumClass.EventNotifyEnum, Action<object>> _eventHandlers = new Dictionary<GameObjEnumClass.EventNotifyEnum, Action<object>>();


        #region 事件注册
        public void InvokeEvent(GameObjEnumClass.EventNotifyEnum eventNotifyEnum, Action<object> action, object args)
        {
            if (!_eventHandlers.ContainsKey(eventNotifyEnum))
            {
                // 没有注册事件
                _eventHandlers.Add(eventNotifyEnum, action);
            }
            _eventHandlers[eventNotifyEnum] += action;
        }

        public void InvokeEvent(GameObjEnumClass.EventNotifyEnum eventNotifyEnum, Action<object> action)
        {
            InvokeEvent(eventNotifyEnum, action, null);
        }
        #endregion

        #region 事件注销
        public void RemoveEvent(GameObjEnumClass.EventNotifyEnum eventNotifyEnum, Action<object> action)
        {
            if (_eventHandlers.ContainsKey(eventNotifyEnum))
            {
                _eventHandlers[eventNotifyEnum] -= action;
            }
        }
        #endregion

        #region 事件调用
        public void CallEventNoArgs(GameObjEnumClass.EventNotifyEnum eventNotifyEnum)
        {
            CallEventNoArgs(eventNotifyEnum, null);
        }
        public void CallEventNoArgs(GameObjEnumClass.EventNotifyEnum eventNotifyEnum, object args)
        {
            if (_eventHandlers.ContainsKey(eventNotifyEnum))
            {
                _eventHandlers[eventNotifyEnum]?.Invoke(args);
            }
        }

        #endregion
    }
}
